package cn.summit.response;


import cn.summit.constants.CommonConstants;

import java.io.Serializable;

/**
 * 前端端统一结果返回
 *
 * @author summit
 * @since 2020/6/24 10:04
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 4365830381500645685L;

    /**
     * 成功标志
     */
    private Boolean success;

    /**
     * code值
     */
    private String code;

    /**
     * 描述信息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    public static <T> Result<T> success(T data) {
        return build(true, CommonConstants.SUCCESS_CODE, null, data);
    }

    public static <T> Result<T> failed(String message) {
        return failed(CommonConstants.FAILED_CODE, message);
    }

    public static <T> Result<T> failed(String code, String message) {
        return build(false, code, message, null);
    }

    public static <T> Result<T> failed(String code, String message,T data) {
        return build(false, code, message, data);
    }


    public static <T> Result<T> build(Boolean success, String code, String message, T data) {
        return new Result<>(success, code, message, data);
    }



    public Result(Boolean success, String code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public void makeSuccess() {
        this.success = true;
        this.code = CommonConstants.SUCCESS_CODE;
    }

    public void makeSuccess(T data) {
        makeSuccess();
        this.setData(data);
    }

    public void makeFailed(String code, String message) {
        this.success = false;
        this.code = code;
        this.message = message;
    }

    public void makeFailed(String message) {
        this.success = false;
        this.code = CommonConstants.FAILED_CODE;
        this.message = message;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
